#define _CRT_SECURE_NO_WARNINGS 1

  struct ListNode {
 	int val;
 	struct ListNode *next;
 	ListNode(int x) : val(x), next(nullptr) {}
  };
 
class Solution
{
public:
    ListNode* Reversal(ListNode* head1)
    {
        ListNode* cur = head1;
        ListNode* prev = nullptr;
        while (cur)
        {
            ListNode* tmp = cur->next;
            cur->next = prev;
            prev = cur;
            cur = tmp;
        }
        return prev;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2)
    {
        head1 = Reversal(head1);
        head2 = Reversal(head2);
        ListNode* cur1 = head1;
        ListNode* cur2 = head2;
        ListNode* ret = new ListNode(-1);
        ListNode* tail = ret;
        int carry = 0;
        while (cur1 && cur2)
        {
            int sum = cur1->val + cur2->val + carry;
            carry = sum / 10;
            tail->next = new ListNode(sum % 10);
            tail = tail->next;
            cur1 = cur1->next;
            cur2 = cur2->next;
        }
        ListNode* cur3 = (cur1 == nullptr ? cur2 : cur1);
        while (cur3)
        {
            int sum = cur3->val + carry;
            carry = sum / 10;
            tail->next = new ListNode(sum % 10);
            tail = tail->next;
            cur3 = cur3->next;
        }
        if (carry != 0)
        {
            tail->next = new ListNode(carry);
        }
        ListNode* del = ret;
        ret = ret->next;
        delete del;
        ret = Reversal(ret);
        return ret;
    }
};